package org.example.filter;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;

/**
 * 请求上下文
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class GatewayContext {

    public static final String CACHE_GATEWAY_CONTEXT = "cacheGatewayContext";

    private Long startTime;
    /**
     * cache json body
     */
    private String cacheBody;
    /**
     * cache formdata,表单数据
     */
    private MultiValueMap<String, String> formData;
    /**
     * url数据，拼接在路径上的参数
     */
    private MultiValueMap<String, String> urlData;
    /**
     * cache reqeust path
     */
    private String path;


    public static GatewayContext getContext(ServerWebExchange exchange) {
        GatewayContext gatewayContext = exchange.getAttribute(GatewayContext.CACHE_GATEWAY_CONTEXT);
        if (gatewayContext == null) {
            gatewayContext = new GatewayContext();
        }
        return gatewayContext;
    }
}
